package com.allyes.flume.interceptor;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

import com.allyes.flume.interceptor.rawlogdigger.DiggerInfoFetcher;
import org.apache.flume.Event;

import com.allyes.flume.interceptor.rawlogdigger.RawlogDigger;
import com.allyes.flume.interceptor.rawlogdigger.RawlogDiggerFactory;
import com.google.common.base.Preconditions;

public class DynamicRawlogEventHeader {
  private List<RawlogDigger> diggers = new LinkedList<RawlogDigger>();

  public void initialize(HashSet<String> headers) {
    diggers.clear();
    for (String h: headers) {
      RawlogDigger digger = RawlogDiggerFactory.newInstance(h);
      if (digger != null) {
        diggers.add(digger);
      } else {
        Preconditions.checkState(false, "Invalid dynamic header in RawlogInterceptor: " + h);
      }
    }
  }

  public void updateHeader(Event event, DiggerInfoFetcher log) {
    for (RawlogDigger digger: diggers) {
      event.getHeaders().put(digger.key(), digger.value(log));
    }
  }

}
